//import org.reactivestreams.Publisher;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.core.io.buffer.DataBufferFactory;
//import org.springframework.core.io.buffer.DataBufferUtils;
//import org.springframework.http.ResponseCookie;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
//import org.springframework.util.MultiValueMap;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.net.URI;
//import java.util.ArrayList;
//import java.util.List;
//
//// gateway过滤器
//class AccesslogGlobalFilter implements GlobalFilter, Ordered {
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpResponse originalResponse = exchange.getResponse();
//        DataBufferFactory bufferFactory = originalResponse.bufferFactory();
//        ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
//            @Override
//            public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
//                if (body instanceof Flux) {
//                    Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;
//                    //解决返回体分段传输,防止日志数据丢失
//                    return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
//                        List<String> list = new ArrayList();
//                        byte[] contentByte = new byte[0]; // 直接接收dataBuffers的字节，不需要做转码
//                        for (DataBuffer dataBuffer : dataBuffers
//                        ) {
//                            byte[] content = new byte[dataBuffer.readableByteCount()];
//                            dataBuffer.read(content);
//                            DataBufferUtils.release(dataBuffer);
//                            byte[] newByte = contentByte;
//                            contentByte = new byte[newByte.length + content.length];
//                            System.arraycopy(newByte, 0, contentByte, 0, newByte.length);
//                            System.arraycopy(content, 0, contentByte, newByte.length, content.length);
//                            try {
//                                list.add(new String(content, "utf-8"));
//                            } catch (Exception e) {
//                            }
//                        }
//                        return bufferFactory.wrap(contentByte);
//                    }));
//                    // 感谢阅读，不用再看“万恶的别人代码”
//                }
//                return super.writeWith(body);
//            }
//
//            @Override
//            public MultiValueMap<String, ResponseCookie> getCookies() {
//                return super.getCookies();
//            }
//        };
//        return null;
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//}